﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.ClickCricket.bll;

/// <summary>
/// Summary description for PlayerLinqProvider
/// </summary>
/// 
namespace com.ClickCricket.dal
{
    public class PlayerLinqProvider : IPlayerProvider
    {
        public PlayerLinqProvider()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Player getRandomPlayer()
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {

                System.Data.Linq.Table<Entity.PlayerTable> playersTable = dc.GetTable<Entity.PlayerTable>();

                int numberOfPlayers = playersTable.Count<Entity.PlayerTable>();
                List<int> playerIds = new List<int>();
                
                foreach (var varplayer in playersTable)
                {
                    playerIds.Add(varplayer.PlayerID);
                }
                if (playerIds.Count == 0)
                    return null;
                Random rand=new Random();

                int randomPlayerId = playerIds[rand.Next(0, playerIds.Count)];
                var player = from aPlayer in dc.PlayerTables
                             where aPlayer.PlayerID == randomPlayerId
                             select aPlayer;
                if (player == null)
                {
                    return null;
                }
                if (player.Count<Entity.PlayerTable>() == 1)
                {
                    return extractPlayer(player);
                }
                throw new Exception(" ");

                            
                            
            }
        }

        public Player getPlayer(int playerId)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                var player = from aPlayer in dc.PlayerTables
                              where aPlayer.PlayerID==playerId
                              select aPlayer;
                if (player == null)
                {
                    return null;
                }
                if (player.Count<Entity.PlayerTable>() == 1)
                {
                    return extractPlayer(player);
                }
                throw new Exception("Invalid Player Id " + playerId + " !");



            }
        }

        public List<Player> getPlayers(string letter)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                List<Player> _listPlayer = new List<Player>();
                var players = (from aPlayer in dc.PlayerTables
                              where aPlayer.PlayerFirstName.StartsWith(letter)
                              select aPlayer).ToList<Entity.PlayerTable>();
                foreach (var player in players)
                {
                    _listPlayer.Add(extractPlayer(player));
                }
                return _listPlayer;
            }
        }

        private Player extractPlayer(Entity.PlayerTable player)
        {
            Player aPlayer = new Player(
            player.PlayerID,
            player.PlayerFirstName,
            player.PlayerLastName,
            player.DateofBirth,
            player.CountryID,
            player.PlayerNickName,
            player.PlayerPic);
            return aPlayer;


        }

        private Player extractPlayer(IQueryable<Entity.PlayerTable> player)
        {
            Player aPlayer = new Player(
                player.Single<Entity.PlayerTable>().PlayerID,
                player.Single<Entity.PlayerTable>().PlayerFirstName,
                player.Single<Entity.PlayerTable>().PlayerLastName,
                player.Single<Entity.PlayerTable>().DateofBirth,
                player.Single<Entity.PlayerTable>().CountryID,
                player.Single<Entity.PlayerTable>().PlayerNickName,
                player.Single<Entity.PlayerTable>().PlayerPic);
            return aPlayer;
        }
    }
}
